package com.gy.assistant.ui.meeting;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import java.util.ArrayList;

import im.zego.zim.callback.ZIMCallInvitationListQueriedCallback;
import im.zego.zim.entity.ZIMCallInfo;
import im.zego.zim.entity.ZIMCallInvitationQueryConfig;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.assistant.R;
import com.gy.assistant.SDKManager;
import com.gy.assistant.ui.BaseActivity;
import com.gy.assistant.ui.meeting.adapter.MeetingListAdapter;

public class MeetingListActivity extends BaseActivity {

    private long mNextFlag = 0;
    private MeetingListAdapter mAdapter;
    private LinearLayoutManager linearLayoutManager;
    private boolean isLoading = false;

    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, MeetingListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meeting_list);
        setTitle("会议历史记录");
        mAdapter = new MeetingListAdapter();
        RecyclerView meetingList = findViewById(R.id.rv_meeting_list);
        meetingList.setAdapter(mAdapter);
        linearLayoutManager = new LinearLayoutManager(this);
        meetingList.setLayoutManager(new LinearLayoutManager(this));
        meetingList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == mAdapter.getItemCount()) {
                    if (!isLoading) {
                        isLoading = true;
                        queryCallList();
                    }
                }
            }
        });

        mAdapter.setClickListener(new MeetingListAdapter.IClickListener() {
            @Override
            public void click(ZIMCallInfo callInfo) {
                MeetingDetailActivity.actionStart(MeetingListActivity.this,callInfo);
            }
        });

        queryCallList();
    }

    private void queryCallList() {
        ZIMCallInvitationQueryConfig config = new ZIMCallInvitationQueryConfig();
        config.count = 30;
        config.nextFlag = (int) mNextFlag;
        SDKManager.sharedInstance().queryCallList(config, new ZIMCallInvitationListQueriedCallback() {
            @Override
            public void onCallInvitationListQueried(ArrayList<ZIMCallInfo> callList, long nextFlag, ZIMError errorInfo) {
                if (errorInfo.code == ZIMErrorCode.SUCCESS) {
                    mNextFlag = nextFlag;
                    isLoading = false;
                    mAdapter.addDataAndNotify(callList);
                }else {
                    toast("onCallListQueried error,show the debug log");
                }
            }

        });
    }
}